<?php
$id			= JRequest::getInt('id', 0, 'GET');
$document	=& JFactory::getDocument();
$user 		= &JFactory::getUser();

$db = JFactory::getDbo();
$sql 	= $db->getQuery(true);

$sql->select("title,id");
$sql->from("#__categories");
$sql->where("id=".$id);
$db->setQuery($sql);
$rowCate = $db->loadObject();

$title = new JConfig();
$document->setTitle($title->sitename." - ".$rowCate->title);

$query 	= $db->getQuery(true);
$query->select("id");
$query->from("#__categories");
$query->where("parent_id=".$id);
//echo $query;
$db->setQuery($query);
$lsCate = $db->loadObjectList();
if(count($lsCate)>0){
	$where = " AND catid IN(".$id.",";
	$i=0;
	foreach ($lsCate as $cat) {
		$i++;
		if ($i == count($lsCate)) $str = ""; else $str = ",";
		$where .= $cat->id.$str;
	}
	$where .= ")";
}else{
	$where = " AND catid=".$id;
}
//echo $where;

$numberPage = 10;

$query 	= $db->getQuery(true);
$query->select("id,title,title_alias,introtext,created");
$query->from("#__content");
$query->where("state=1".$where);
$query->order("ordering ASC, id DESC LIMIT 0,".$numberPage);
//echo $query;
$db->setQuery($query);
$rows = $db->loadObjectList();
//echo $query;
?>
<div class="componentheading"><?php echo $rowCate->title;?></div>
<?php
if (count($rows)<5){
	?>
	<table id="show_content_text" class="blog" cellspacing="0" cellpadding="0">
	<?php
	$nows = mktime(0,0,0,date("m"),date("d"),date("Y"));
	$i=0;
	foreach ($rows as $row) {
		$i++;
		$ngay_nhap = mktime(0,0,0,unFormatdate($row->created,"m"),unFormatdate($row->created,"d"),unFormatdate($row->created,"Y"));				
		$days = ($nows - $ngay_nhap)/86400;
		if ($days>0){
			$class = '';
		}else{
			$class = ' news_title';
		}
		$link = JRoute::_("index.php?option=com_content&view=article&id=".$row->id);
		//attachment
		$sql = $db->getQuery(true);
		$sql->select("filename,url,display_name,description,icon_filename,access");
		$sql->from("#__attachments");
		$sql->where("parent_id=".$row->id." AND state=1");
		$sql->order("id ASC");
		$db->setQuery($sql);
		$ats = $db->loadObjectList();
		if ($i==1) $first = ' first'; else $first = '';
		?>
		<tr>
			<td width="100%" valign="top">
				<div>
				<table class="contentpaneopen">
					<tbody>
						<tr>
							<td width="100%" class="contentheading">
							<a href="<?php echo $link;?>" class="contentpagetitle<?php echo $class;?>"><?php echo $row->title;?></a>									
							</td>
						</tr>
					</tbody>
				</table>
				<table class="contentpaneopen<?php echo $first;?>">
					<tbody>
						<tr>
							<td valign="top" colspan="2">
								<?php echo $row->introtext;?>
								<?php
								if (count($ats)>0){
									echo '<p><b>File đính kèm:</b> ';
									foreach ($ats as $at) {
										if ($at->display_name!='') $name_file = $at->display_name; else $name_file = $at->filename;
										if ($user->id>0 && $at->access == 2){
											echo '<a style="color:#b73836; padding-right:10px;" href="'.$this->baseurl."/".$at->url.'">'.$name_file.'</a>';
										}
										if($at->access == 1){
											echo '<a style="color:#b73836; padding-right:10px;" href="'.$this->baseurl."/".$at->url.'">'.$name_file.'</a>';
										}
									}
									echo '</p>';
								} 
								?>
							</td>
						</tr>
						<tr>
							<td align="right" style="border-bottom:1px solid #DBDBDB;" colspan="2">
								<a class="readon" href="<?php echo $link;?>">Xem tiếp...</a>
							</td>
						</tr>
					</tbody>
				</table>
				<span class="article_seperator">&nbsp;</span>
				</div>
			</td>
		</tr>
		<?php
	}
	?>
	</table>
	<?php
}else{
	?>
	<table id="show_content_text" class="blog" cellspacing="0" cellpadding="0">
		<?php
		$nows = mktime(0,0,0,date("m"),date("d"),date("Y"));
		for ($i=0; $i<5; $i++){
			$ngay_nhap = mktime(0,0,0,unFormatdate($rows[$i]->created,"m"),unFormatdate($rows[$i]->created,"d"),unFormatdate($rows[$i]->created,"Y"));				
			$days = ($nows - $ngay_nhap)/86400;
			if ($days>0){
				$class = '';
			}else{
				$class = ' news_title';
			}
			$link = JRoute::_("index.php?option=com_content&view=article&id=".$rows[$i]->id);
			//attachment
			$sql = $db->getQuery(true);
			$sql->select("filename,url,display_name,description,icon_filename,access");
			$sql->from("#__attachments");
			$sql->where("parent_id=".$rows[$i]->id." AND state=1");
			$sql->order("id ASC");
			$db->setQuery($sql);
			$ats = $db->loadObjectList();
			if ($i==0) $first = ' first'; else $first = '';
			?>
			<tr>
				<td width="100%" valign="top">
					<div>
					<table class="contentpaneopen">
						<tbody>
							<tr>
								<td width="100%" class="contentheading">
								<a href="<?php echo $link;?>" class="contentpagetitle<?php echo $class;?>"><?php echo $rows[$i]->title;?></a>									
								</td>
							</tr>
						</tbody>
					</table>
					<table class="contentpaneopen<?php echo $first;?>">
						<tbody>
							<tr>
								<td valign="top" colspan="2">
									<?php echo $rows[$i]->introtext;?>
									<?php
									if (count($ats)>0){
										echo '<p><b>File đính kèm:</b> ';
										foreach ($ats as $at) {
											if ($at->display_name!='') $name_file = $at->display_name; else $name_file = $at->filename;
											if ($user->id>0 && $at->access == 2){
												echo '<a style="color:#b73836; padding-right:10px;" href="'.$this->baseurl."/".$at->url.'">'.$name_file.'</a>';
											}
											if($at->access == 1){
												echo '<a style="color:#b73836; padding-right:10px;" href="'.$this->baseurl."/".$at->url.'">'.$name_file.'</a>';
											}
										}
										echo '</p>';
									} 
									?>
								</td>
							</tr>
							<tr>
								<td align="right" style="border-bottom:1px solid #DBDBDB;" colspan="2">
									<a class="readon" href="<?php echo $link;?>">Xem tiếp...</a>
								</td>
							</tr>
						</tbody>
					</table>
					<span class="article_seperator">&nbsp;</span>
					</div>
				</td>
			</tr>
			<?php
		}
		?>
		</table>
		<div class="clearfix"></div>
		<?php
		if($total>$numberPage){
			?>
			<div class="page">
			<?php
			echo $pageNav->writePagesLinks( $link_page );
			?>
			</div>
			<?php
		}
		?>
		<div class="clearfix"></div>
		<div class="blog_more">
			<div><span>Các bài đã đăng</span></div>
			<ul>
				<?php
				$nows = mktime(0,0,0,date("m"),date("d"),date("Y"));
				for ($i=5; $i<count($rows); $i++){
					$ngay_nhap = mktime(0,0,0,unFormatdate($rows[$i]->created,"m"),unFormatdate($rows[$i]->created,"d"),unFormatdate($rows[$i]->created,"Y"));				
					$days = ($nows - $ngay_nhap)/86400;
					if ($days>0){
						$class = '';
					}else{
						$class = ' class="icon_news"';
					}
					$link = JRoute::_("index.php?option=com_content&view=article&id=".$rows[$i]->id);
					//attachment
					$sql = $db->getQuery(true);
					$sql->select("filename,url,display_name,description,icon_filename,access");
					$sql->from("#__attachments");
					$sql->where("parent_id=".$rows[$i]->id." AND state=1");
					$sql->order("id ASC");
					$db->setQuery($sql);
					$ats = $db->loadObjectList();
					echo '<li><a '.$class.'href="'.$link.'">'.$rows[$i]->title.'</a>';
					if (count($ats)>0){
						foreach ($ats as $at) {
							if ($at->display_name!='') $name_file = $at->display_name; else $name_file = $at->filename;
							if ($user->id>0 && $at->access == 2){
								echo ' (<a style="color:#b73836;" href="'.$this->baseurl."/".$at->url.'">'.$name_file.'</a>)';
							}
							if($at->access == 1){
								echo ' (<a style="color:#b73836;" href="'.$this->baseurl."/".$at->url.'">'.$name_file.'</a>)';
							}
						}
					}
					echo '</li>'; 
					echo '<span class="clearfix"></span>';
				} 
				?>
			</ul>
		</div>
		<?php
}	 
?>